package com.example.springsecurity.common.exception;

import com.example.springsecurity.common.resp.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.example.springsecurity.common.constant.Constant.*;
import static com.example.springsecurity.common.enums.GlobalExceptionEnum.ERROR_SERVER_USER_PERMISSIONS_FAIL;

/**
 * @Description：全局异常处理类
 * @Author：fengcheng
 * @Date：2024/12/16 16:44
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    public static String errorCode = String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR);

    /**
     * @Description：全局捕获业务异常
     * @Author：fengcheng
     * @Date：2024/12/16 16:48
     * @Param：e
     * @ParamsType：[com.example.clientweb.common.exception.BizException]
     * @Return：com.example.clientweb.common.response.ResponseBody
     */
    @ExceptionHandler(value = BizException.class)
    public <T> ResponseResult<T> handleBizException(BizException e) {
        log.error(GLOBAL_EXCEPTION_MSG + "{}" + BUSINESS_EXCEPTION_MSG + "{}{}", MID_BAR, MID_BAR, e.getMessage(), e);
        return ResponseResult.buildResponseResult(errorCode, BUSINESS_ERROR_MSG + MID_BAR + e.getMessage());
    }

    /**
     * @Description：全局捕获运行时异常
     * @Author：fengcheng
     * @Date：2024/12/16 16:49
     * @Param：e
     * @ParamsType：[java.lang.RuntimeException]
     * @Return：com.example.clientweb.common.response.ResponseBody
     */
    @ExceptionHandler(value = RuntimeException.class)
    public <T> ResponseResult<T> handleRuntimeException(RuntimeException e) {
        log.error(GLOBAL_EXCEPTION_MSG + "{}" + RUNTIME_EXCEPTION_MSG + "{}{}", MID_BAR, MID_BAR, e.getMessage(), e);
        return ResponseResult.buildResponseResult(errorCode, e.getMessage().equals("不允许访问") ? ERROR_SERVER_USER_PERMISSIONS_FAIL.getMessage() : SYSTEM_ERROR_MSG);
    }

    /**
     * @Description：全局捕获空指针异常
     * @Author：fengcheng
     * @Date：2024/12/16 16:49
     * @Param：e
     * @ParamsType：[java.lang.NullPointerException]
     * @Return：com.example.clientweb.common.response.ResponseBody
     */
    @ExceptionHandler(value = NullPointerException.class)
    public <T> ResponseResult<T> handleNullPointerException(NullPointerException e) {
        log.error(GLOBAL_EXCEPTION_MSG + "{}" + NULL_EXCEPTION_MSG + "{}{}", MID_BAR, MID_BAR, e.getMessage(), e);
        return ResponseResult.buildResponseResult(errorCode, CODE_ERROR_MSG + MID_BAR + e.getMessage());
    }

    /**
     * @Description：处理实体类参数注解的异常
     * @Author：fengcheng
     * @Date：2024/12/16 16:49
     * @Param：e
     * @ParamsType：[org.springframework.http.converter.HttpMessageNotReadableException]
     * @Return：com.example.clientweb.common.response.ResponseBody
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public <T> ResponseResult<T> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error(GLOBAL_EXCEPTION_MSG + "{}" + PARAM_EXCEPTION_MSG + "{}{}", MID_BAR, COLON, e.getMessage(), e);
        return ResponseResult.buildResponseResult(errorCode, CHECK_ERROR_MSG + COLON + PARAM_EXCEPTION_MSG);
    }

    /**
     * @Description：全局捕获其他异常
     * @Author：fengcheng
     * @Date：2024/12/16 16:49
     * @Param：e
     * @ParamsType：[java.lang.Exception]
     * @Return：com.example.clientweb.common.response.ResponseBody
     */
    @ExceptionHandler(value = Exception.class)
    public <T> ResponseResult<T> handleException(Exception e) {
        log.error(GLOBAL_EXCEPTION_MSG + "{}", e.getMessage(), e);
        return ResponseResult.buildResponseResult(errorCode, SYSTEM_ERROR_MSG);
    }
}
